package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import com.yalantis.phoenix.PullToRefreshView;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import yalantis.com.sidemenu.interfaces.Resourceble;

public class MyCloud extends AppCompatActivity {

    private ListView mylistview;
    List<String> NoteContent;
    List<String> NoteId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_cloud);

        PullToRefreshView mPullToRefreshView = (PullToRefreshView) findViewById(R.id.cloud_pull_to_refresh);
        mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPullToRefreshView.setRefreshing(false);
                    }
                }, 1000);
            }
        });
        mylistview = (ListView) findViewById(R.id.cloud_listView);
        try {
            mylistview.setAdapter(new ItemAdapter(this, getItem()));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        AdapterView.OnItemClickListener lvListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                int NoteNo=Integer.valueOf(NoteId.get(arg2));
                String NoteContent1=NoteContent.get(arg2);
                Intent intent=new Intent(MyCloud.this,MyCloudDetails.class);
                intent.putExtra("NoteID",NoteNo);
                intent.putExtra("Content",NoteContent1);
                startActivity(intent);
//                if (arg2==0){
//                    startActivity(new Intent(SelectItem.this,MainActivity.class));
//                }
//                else if (arg2==1){//备忘录
//                    startActivity(new Intent(SelectItem.this,Mono_activity.class));
//                }
//                else if (arg2==2){//云
//                    startActivity(new Intent(SelectItem.this,CloudItem.class));
//                }
//                else if (arg2==3){//我的
//                    startActivity(new Intent(SelectItem.this,MainActivity.class));
//                }
            }
        };
        mylistview.setOnItemClickListener(lvListener);

        //要显示的数据
    }

    private List<Item> getItem() throws ClassNotFoundException, SQLException {
        List<Item> Animals = new ArrayList<>();
        //在这里进行查询
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String Url="jdbc:mysql://cdb-l1c6g0ne.bj.tencentcdb.com:10166/AndroidUser?user=root&password=tzyTZY123";
                    String Driver="com.mysql.jdbc.Driver";
                    Connection connection = null;
                    PreparedStatement preparedStatement = null;
                    ResultSet resultSet;
                    Class.forName(Driver);
                    connection = DriverManager.getConnection(Url);
                    String MySql = "select * from Note where UserName=?";//?代表参数会在以后加
//            String MySql = "select * from android_user";
                    preparedStatement = connection.prepareStatement(MySql);
                    preparedStatement.setString(1, LoginWithanim.LoginUserName);//代表第一个？放置这个参数，该用户的所有笔记本
                    resultSet =  preparedStatement.executeQuery();
                    //遍历查询结果集
                    NoteContent=new ArrayList<>();
                    NoteId=new ArrayList<>();
                    while(resultSet.next()){
                        NoteContent.add(resultSet.getString("Content"));
                        NoteId.add(resultSet.getString("NoteNo"));
                    }
                    for(int i=0;i<NoteContent.size();i++){
                        Animals.add(new Item(NoteContent.get(i),R.mipmap.cloud));
                    }
                    System.out.println(Animals);
                } catch (ClassNotFoundException | SQLException e) {
                    e.printStackTrace();
                }
                //2.创建连接
            }
        }).start();
        System.out.println(Animals);
//        Item an1=new Item("MyNote",R.mipmap.notebook);
//        Item an2=new Item("MyMemo",R.mipmap.mono);
//        Item an3=new Item("MyCloud",R.mipmap.cloud);
//        Item an4=new Item("Funny",R.mipmap.found);
//        Item an5=new Item("Me",R.mipmap.me);
//        Animals.add(an1);
//        Animals.add(an2);
//        Animals.add(an3);
//        Animals.add(an4);
//        Animals.add(an5);
        return Animals;
    }

    public class ItemAdapter extends ArrayAdapter<Item> {
        private List<Item> Animals;

        public ItemAdapter(Context context, List<Item> Animals) {
            super(context, 0, Animals);
            this.Animals = Animals;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.begin_item, null);
            TextView Name = (TextView) convertView.findViewById(R.id.item_name);
            ImageView picture = (ImageView) convertView.findViewById(R.id.item_image);
            Item anAnimal = Animals.get(position);
            Name.setText(anAnimal.getName());
            picture.setImageResource( anAnimal.getImageResourceId());
            return convertView;
        }
    }


}